package com.rongxun.hbwallet.ui.activities;

import android.content.Context;
import android.os.Build;
import android.support.annotation.IntRange;
import android.support.annotation.NonNull;
import android.support.annotation.RequiresApi;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.rongxun.base.HomeFragVpAdapter;
import com.rongxun.base.MvpActivity;
import com.rongxun.hbwallet.MyApp;
import com.rongxun.hbwallet.R;
import com.rongxun.hbwallet.mvp.contract.IMain;
import com.rongxun.hbwallet.mvp.model.ApiModel;
import com.rongxun.hbwallet.mvp.presenter.MainPresenter;
import com.rongxun.hbwallet.ui.fragment.CommunityFragment;
import com.rongxun.hbwallet.ui.fragment.ExcavateFragment;
import com.rongxun.hbwallet.ui.fragment.HomeFragment;
import com.rongxun.hbwallet.ui.fragment.MySelfFragment;
import com.rongxun.hbwallet.ui.fragment.TransactionFragment;
import com.rongxun.utils.EventUtil;
import com.rongxun.utils.InfoEvent;
import com.rongxun.widget.CustomViewPager;
import com.yinglan.alphatabs.AlphaTabsIndicator;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;

public class MainActivity extends MvpActivity<IMain.IView, MainPresenter> implements IMain.IView {
    @BindView(R.id.main_boottomvavigationbar)
    AlphaTabsIndicator mBoottomvavigationbar;
    @BindView(R.id.main_viewpager)
    CustomViewPager    mViewpager;
    private int             setlectIndex;
    private FragmentManager fragmentManager;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void init() {
        EventUtil.register(this);

        fragmentManager = getSupportFragmentManager();

        initViewPager();

        doOnTabSelected(setlectIndex);

        getPresenter().refreshToken();

    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    private void initViewPager() {
        final String[] fnames = {
                HomeFragment.class.getName(),
                TransactionFragment.class.getName(),
                ExcavateFragment.class.getName(),
                CommunityFragment.class.getName(),
                MySelfFragment.class.getName()
        };
        HomeFragVpAdapter adapter = new HomeFragVpAdapter(getSupportFragmentManager(), this, fnames);
        mViewpager.setOffscreenPageLimit(5);
        mViewpager.setScanScroll(false);
        mViewpager.setAdapter(adapter);
        mViewpager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setlectIndex = position;
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        mBoottomvavigationbar.setViewPager(mViewpager);
        mBoottomvavigationbar.setOnTabChangedListner(tabNum -> doOnTabSelected(tabNum));
        mBoottomvavigationbar.setTabCurrenItem(0);
    }


    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public void doOnTabSelected(@IntRange(from = 0, to = 3) int position) {
        setlectIndex = position;
        switch (position) {
            case 0:
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

                mViewpager.setCurrentItem(0, false);
                break;
            case 1:
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

                mViewpager.setCurrentItem(1, false);
                break;
            case 2:
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

                mViewpager.setCurrentItem(2, false);
                break;
            case 3:
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
                mViewpager.setCurrentItem(3, false);
                break;
            case 4:
                getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
                mViewpager.setCurrentItem(4, false);
                break;
        }
    }

    @Override
    protected int getContentView() {
        return R.layout.activity_main;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    public void showLoading(boolean isShow) {

    }

    @Override
    public void showError(String msg, String errorCode) {

    }

    @Override
    public void checkPermissionsSuccessful(String... permissions) {

    }

    @Override
    public void release() {
    }

    @NonNull
    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter(this,new ApiModel(this));
    }

    private double firstTime;

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 2000) {
                Toast.makeText(this, getString(R.string.double_click), Toast.LENGTH_SHORT).show();
                firstTime = secondTime;
                return true;
            } else {
                MyApp.getInstance().exitApp();

            }
        }

        return super.onKeyUp(keyCode, event);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(InfoEvent event) {

    }
}

